import {
  Body,
  Controller,
  Delete,
  Get,
  ParseIntPipe,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { CategoryService } from './category.service';
import { ResponseData, ResponseDataProps } from 'src/common/response.data';
import { CategoryDto } from './dto/category.dto';
import { ApiBearerAuth } from '@nestjs/swagger';

@ApiBearerAuth()
@Controller('category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @Get()
  async list(): Promise<ResponseDataProps> {
    return ResponseData.success(await this.categoryService.findAll());
  }
  @Post()
  async create(
    @Req() reg: any,
    @Body() body: CategoryDto,
  ): Promise<ResponseDataProps> {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
    body.create_user = reg.user.userid;
    return ResponseData.success(await this.categoryService.create(body));
  }

  @Get('detail')
  async detail(
    @Query('id', ParseIntPipe) id: number,
  ): Promise<ResponseDataProps> {
    return ResponseData.success(await this.categoryService.findOneBy({ id }));
  }

  @Put()
  async update(@Body() body: CategoryDto): Promise<ResponseDataProps> {
    return ResponseData.success(await this.categoryService.update(body));
  }

  @Delete()
  async delete(
    @Query('id', ParseIntPipe) id: number,
  ): Promise<ResponseDataProps> {
    return ResponseData.success(await this.categoryService.delete(id));
  }
}
